﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExplorePanelData
{
    public List<UserMomentsInfo> candidates;
}

public class ExplorePanel : AppUI
{
    public Transform panelsParent;

    public CandidateItem panelPref;

    List<CandidateItem> m_Panels = new List<CandidateItem>();

    ExplorePanelData m_Data;

    public ItemListDispersed listDispersed;

    //public ItemListManager ItemListManager;
    public void Init(ExplorePanelData explorePanelData)
    {
        if (m_Data != explorePanelData)
        {
            UnityFunctions.ClearList(m_Panels);

            for (int i = 0; i < explorePanelData.candidates.Count; i++)
            {
                if (explorePanelData.candidates[i] == null)
                    continue;

                var item = Instantiate(panelPref, panelsParent);
                item.gameObject.SetActive(true);
                item.Init(explorePanelData.candidates[i].user,OnSelect);
                m_Panels.Add(item);
            }

            m_Data = explorePanelData;
        }

        if (listDispersed != null)
            listDispersed.maxIndex = explorePanelData.candidates.Count - 1;
    }

    // Start is called before the first frame update
    public override void Start()
    {
        base.Start();

        panelPref.gameObject.SetActive(false);
    }

    private void OnSelect(CandidateItem item)
    {
        GameDebug.Log($"OnSelect:{item.Id}");

        var ui = UIController.ShowUI<UserHomePanel>();
        ui.Init(this.m_Data.candidates.Find(e=>e.user.userId == item.Id));

        RequestServerController.instance.ReqVisit(MGame.instance.MUser.id, item.Id, (v) =>
        {
            GameDebug.Log($"ReqVisit {item.Id} end");
        });
    }

    public void OnClickMyRequirement()
    {
        GameDebug.Log("MyPagePanel->OnClickRequirement");
        var require = GetComponentInParent<UIController>().ShowUI<EditRequirementPanel>();
        require.Init(MGame.instance.MUser.requirement);
    }


    // Update is called once per frame
    void Update()
    {
        
    }
}
